Passed
Push — master ( d4464b...8d34d2 )
by Mathieu
01:55
created

CreateDailyRateAction.index   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
c 0
b 0
f 0
rs 9.8
cc 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import {AuthGuard} from '@nestjs/passport';
10
import {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {ICommandBus} from 'src/Application/ICommandBus';
12
import {CreateDailyRateCommand} from 'src/Application/Accounting/Command/DailyRate/CreateDailyRateCommand';
13
import {DailyRateDTO} from '../DTO/DailyRateDTO';
14
15
@Controller('daily_rates')
16
@ApiUseTags('Accounting')
17
@ApiBearerAuth()
18
@UseGuards(AuthGuard('bearer'))
19
export class CreateDailyRateAction {
20
  constructor(
21
    @Inject('ICommandBus')
22
    private readonly commandBus: ICommandBus
23
  ) {}
24
25
  @Post()
26
  @ApiOperation({title: 'Create new daily rate'})
27
  public async index(@Body() dto: DailyRateDTO) {
28
    try {
29
      const {userId, customerId, taskId, amount} = dto;
30
      const id = await this.commandBus.execute(
31
        new CreateDailyRateCommand(amount, userId, customerId, taskId)
32
      );
33
34
      return {id};
35
    } catch (e) {
36
      throw new BadRequestException(e.message);
37
    }
38
  }
39
}
40